package com.derbysoft.lowcostadapter.web.action.admin.cachemanager.search;

import com.derbysoft.lowcostadapter.commons.model.RoomOccupancy;
import com.derbysoft.lowcostadapter.commons.redis.service.OccupancyCacheService;
import com.derbysoft.lowcostadapter.commons.redis.utils.KeyValueHelper;
import com.derbysoft.lowcostadapter.web.action.BaseAdapterAction;
import com.derbysoft.lowcostadapter.web.action.model.RoomOccupancyCondition;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: jason
 * Date: 2012-09-26
 */
public class RoomOccupancySearchAction extends BaseAdapterAction {

    private RoomOccupancyCondition roomOccupancyCondition;

    @Autowired
    @Qualifier("occupancyCacheService")
    private OccupancyCacheService occupancyCacheService;

    @Action("searchRoomOccupancy")
    public void searchRoomOccupancy() {
        try {
            roomOccupancyCondition.validate();
            List<RoomOccupancy> roomOccupancies = filter(occupancyCacheService.loadAll(roomOccupancyCondition.makeKey()));
            StringBuilder stringBuilder = new StringBuilder();
            for (RoomOccupancy roomOccupancy : roomOccupancies) {
                stringBuilder.append(roomOccupancy.toFiled()).append(KeyValueHelper.SEPARATOR);
                stringBuilder.append(roomOccupancy.value());
                stringBuilder.append("\n");
            }
            process(StringUtils.isBlank(stringBuilder.toString()) ? NOT_FOUND : stringBuilder.toString());
        } catch (Exception e) {
            populateFailedJsonResult(e);
        }
    }

    private List<RoomOccupancy> filter(List<RoomOccupancy> roomOccupancies) {
        List<RoomOccupancy> availOccupancies = new ArrayList<RoomOccupancy>();
        for (RoomOccupancy roomOccupancy : roomOccupancies) {
            if (check(roomOccupancy)) {
                availOccupancies.add(roomOccupancy);
            }
        }
        return availOccupancies;
    }

    private boolean check(RoomOccupancy roomOccupancy) {
        if (StringUtils.isNotBlank(roomOccupancyCondition.getStart())
                && StringUtils.isNotBlank(roomOccupancyCondition.getEnd())) {
            if (roomOccupancyCondition.getStart().compareTo(roomOccupancy.getStart()) < 0
                    || roomOccupancyCondition.getEnd().compareTo(roomOccupancy.getEnd()) > 0) {
                return false;
            }
        }
        if (StringUtils.isNotBlank(roomOccupancyCondition.getRoomTypeId())) {
            if (!roomOccupancyCondition.getRoomTypeId().equals(String.valueOf(roomOccupancy.getPropertyRoomTypeID()))) {
                return false;
            }
        }
        return true;
    }

    private void process(String value) {
        try {
            populateSucceededJsonResult(value);
        } catch (Exception e) {
            populateFailedJsonResult(e);
        }
    }

    public RoomOccupancyCondition getRoomOccupancyCondition() {
        return roomOccupancyCondition;
    }

    public void setRoomOccupancyCondition(RoomOccupancyCondition roomOccupancyCondition) {
        this.roomOccupancyCondition = roomOccupancyCondition;
    }
}
